import Vue from 'vue';
import axios from "axios";

//1. 配置实例
/* instace = axios.create({
  baseURL: '/api/',
  headers: {'token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFsZXgiLCJfaWQiOiI1ZWZkM2U3NmUzM2ViNDBmNWMyZmZhMDMiLCJpYXQiOjE2MzYwMDc0MjEsImV4cCI6MTYzNjA5MzgyMX0.m1wSoME-KFWdvEOXS5Ah7ztOw9dKxRt7BAMcvSpvE5s'}
}); */

//2. 全局配置
// axios.defaults.baseURL = '/api/';
// axios.defaults.headers.token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFsZXgiLCJfaWQiOiI1ZWZkM2U3NmUzM2ViNDBmNWMyZmZhMDMiLCJpYXQiOjE2MzYwMDc0MjEsImV4cCI6MTYzNjA5MzgyMX0.m1wSoME-KFWdvEOXS5Ah7ztOw9dKxRt7BAMcvSpvE5s"

//3. 拦截器
// 请求拦击
axios.interceptors.request.use(function (config) {
  // 获取登陆时用户的token
  let use = window.localStorage.getItem('user');
  let token = use ? JSON.parse(use).token : '';
  config.headers.token = token;
  config.baseURL = '/api/'

  //控制loading显示
  return config;
}, function (error) {
  // Do something with request error
  return Promise.reject(error);
});

//响应拦截
axios.interceptors.response.use(function (response) {

  // 控制loading隐藏
  return response.data;
}, function (error) {
  // Any status codes that falls outside the range of 2xx cause this function to trigger
  // Do something with response error
  return Promise.reject(error);
});


// 配置axios,将axios绑定到vue实例，axios就作为vue实例的一个属性
Vue.prototype.$axios = axios;

// 默认导出axios,到main.js中引入
export default axios;